Pakistan’s Vaccine Rollout

rm(list=ls())
db <- dbConnect(SQLite(), dbname="../COVID-19-DB/OURWORLD.sqlite3")
df <- dbGetQuery(db,"select * from OWID")
df$date <- as.Date(df$date)
df <- df[order(df$date),]
df <- df %>% select(date,location,iso_code,new_cases,new_deaths,new_vaccinations,
                    new_vaccinations_smoothed,total_vaccinations,population) %>% 
                    filter(iso_code =="PAK") 
             

summary(df$new_vaccinations)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##      NA      NA      NA     NaN      NA      NA     342
summary(df$new_cases)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     0.0   536.8  1135.0  1597.7  2431.2 12073.0
summary(df$new_deaths)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##    0.00    9.00   25.50   36.51   55.25  313.00      22
p5 <- ggplot(df) + geom_line(aes(x=date,y=new_vaccinations)) +
  scale_y_continuous(labels=comma) +
  labs(title="Pakistan's New Vaccinations By Date") +
  geom_smooth(aes(x=date,y=new_vaccinations))
p6 <- ggplot(df) + geom_line(aes(x=date,y=total_vaccinations)) +
  scale_y_continuous(labels=comma) +
  labs(title="Pakistan's Total Vaccinations By Date") 
df$Rate <- df$total_vaccinations/df$population
p7 <- ggplot(df) + geom_line(aes(x=date,y=Rate)) + 
    scale_y_continuous(labels=percent) +  ylim(0,0.5) +
  labs(title="Pakistan's vaccination Rate By Day")
## Scale for 'y' is already present. Adding another scale for 'y', which will
## replace the existing scale.
ggplotly(p5)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 342 rows containing non-finite values (stat_smooth).
ggplotly(p6)
ggplotly(p7)

Pakistan’s Battle with COVID-19

# \
# df <- dbGetQuery(db,"select * from JHU")
# df <- subset(df,location =="Pakistan"  )
# df$date <- as.Date(df$date)
US <- subset(df,date >="2020-02-25" & new_cases >=1)
US <- US[order(US$date,decreasing = TRUE),]
US$MAC <- ma(US$new_cases,7,centre=TRUE)
US$MAD <- ma(US$new_deaths,7,centre = TRUE)
US$Rate <- US$new_deaths/US$new_cases
##
sum(df$new_cases,na.rm=TRUE)
## [1] 546428
sum(df$new_deaths,na.rm=TRUE)
## [1] 11683
mean(df$Rate,na.rm=TRUE)
## [1] NaN

Pakistan COVID19 Mortality Rate

A <- subset(US,date >="2020-07-01")
ggplot(A) + # geom_line(aes(x=date,y=Rate)) +
  scale_y_continuous(labels = scales::percent) +
  labs(title="Pakistan COVID19 Mortality Rate ",x="Date date",y="Mortality Rate") +
  geom_hline(yintercept = mean(A$Rate),col="red") +
  geom_smooth(aes(x=date,y=Rate,col="Loess"),span=0.15) +
  scale_alpha_date()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Plot of Daily Cases and Daily Deaths

plot1 <-ggplot(US) +  
  labs(title="COVID-19 Cases by Date(7 day moving average)",y="Cases") +
    geom_col(aes(x=date,y=MAC)) +
   scale_alpha_date()

plot2 <-ggplot(US) + 
  labs(title="COVID-19 Deaths by Date, (7 day moving average)") + ylim(0,200) +
  geom_col(aes(x=date,y=MAD)) +
   scale_alpha_date()
  

ggplotly(plot1)
## Don't know how to automatically pick scale for object of type ts. Defaulting to continuous.
## Warning: Removed 6 rows containing missing values (position_stack).
ggplotly(plot2)
## Warning: Removed 19 rows containing missing values (position_stack).
# USA <- subset(US,date >="2020-06-01")
# ggplot(USA) + # geom_line(aes(x=date,y=new_cases,col="Daily new_cases")) +
#   labs(title="COVID-19 new_cases by Date since Jun. 1, 2020") +
#   geom_smooth(aes(x=date,y=new_cases,col="Loess"),span=0.25) +
#    scale_alpha_date()
# ggplot(USA) + # geom_line(aes(x=date,y=new_deaths,col="Daily new_deaths")) +
#   labs(title="COVID-19 new_deaths by Date (since Jun. 1, 2020)") + ylim(0,200) +
#   geom_smooth(aes(x=date,y=new_deaths,col="Loess"),span=0.25) +
#    scale_alpha_date()

Non-Moving Average By Week and By Month

US$Monthly <- as.Date(cut(US$date,
  breaks = "month"))
US$Weekly <- as.Date(cut(US$date,
  breaks = "week",
  start.on.monday = FALSE))
Weekly_new_cases <- aggregate(new_cases~Weekly,US,FUN=sum)
Weekly_new_deaths <- aggregate(new_deaths~Weekly,US,FUN=sum)
Weekly_new_cases$DRate <- Weekly_new_deaths$new_deaths/Weekly_new_cases$new_cases
## Warning in Weekly_new_deaths$new_deaths/Weekly_new_cases$new_cases: longer
## object length is not a multiple of shorter object length
Weekly_new_cases$LivedSaved <- Weekly_new_cases$new_cases * (max(Weekly_new_cases$DRate) - Weekly_new_cases$DRate) 
ggplot(Weekly_new_cases) + geom_col(aes(x=Weekly,y=new_cases)) + 
  labs(title="Weekly new_cases",x="Date date", y="Weekly new_cases") +
   scale_alpha_date()

ggplot(Weekly_new_deaths) + geom_col(aes(x=Weekly,y=new_deaths)) + 
  labs(title="Weekly new_deaths",x="Date date", y="Weekly new_deaths") +
   ylim(0,1500) +  scale_alpha_date()

Monthly new_cases and new_deaths

Monthly_new_cases <- aggregate(new_cases~Monthly,US,FUN=sum)
Monthly_new_deaths <- aggregate(new_deaths~Monthly,US,FUN=sum)
Monthly_new_cases$DRate <- Monthly_new_deaths$new_deaths/Monthly_new_cases$new_cases
## Warning in Monthly_new_deaths$new_deaths/Monthly_new_cases$new_cases: longer
## object length is not a multiple of shorter object length
Monthly_new_cases$LivedSaved <- Monthly_new_cases$new_cases * (max(Monthly_new_cases$DRate) - Monthly_new_cases$DRate) * 100
ggplot(Monthly_new_cases) + geom_col(aes(x=Monthly,y=new_cases)) +
  labs(title="Monthly new_cases") +
  scale_y_continuous(labels=scales::comma) +
   scale_alpha_date()

ggplot(Monthly_new_deaths) + geom_col(aes(x=Monthly,y=new_deaths)) +
  labs(title="Monthly new_deaths") +
   scale_alpha_date()